package com.ruoyi.web.controller.video;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.video.service.IVideoCompanyService;
import com.ruoyi.video.service.IVideoDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.video.domain.VideoTemplate;
import com.ruoyi.video.service.IVideoTemplateService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 模板Controller
 * 
 * @author ruoyi
 * @date 2025-06-07
 */
@Controller
@RequestMapping("/video/template")
@Api("模板")
public class VideoTemplateController extends BaseController
{
    private String prefix = "video/template";

    @Autowired
    private IVideoTemplateService videoTemplateService;
    @Autowired
    private IVideoCompanyService companyService;
    @Autowired
    private IVideoDetailService detailService;

//    @RequiresPermissions("video:template:view")
    @GetMapping()
    public String template()
    {
        return prefix + "/template";
    }

    /**
     * 查询模板列表
     */
    @PostMapping("/list")
    @ResponseBody
    @ApiOperation("模板列表分页查询")
    public TableDataInfo list(VideoTemplate videoTemplate)
    {
        startPage();
        Integer companyId = ShiroUtils.getSysUser().getCompanyId();
        if(companyId != null){
            videoTemplate.setCompanyId(companyId);
        }
        List<VideoTemplate> list = videoTemplateService.selectVideoTemplateList(videoTemplate);
        if(CollectionUtils.isNotEmpty(list)){
            list.forEach(e->{
                e.setCompanyName(e.getCompanyId() != null?companyService.selectVideoCompanyById(e.getCompanyId()).getCompanyName():null);
            });
        }
        return getDataTable(list);
    }

    /**
     * 导出模板列表
     */
    @RequiresPermissions("video:template:export")
    @Log(title = "模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(VideoTemplate videoTemplate)
    {
        List<VideoTemplate> list = videoTemplateService.selectVideoTemplateList(videoTemplate);
        ExcelUtil<VideoTemplate> util = new ExcelUtil<VideoTemplate>(VideoTemplate.class);
        return util.exportExcel(list, "模板数据");
    }

    /**
     * 新增模板
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        if(sysUser != null){
            Integer companyId = sysUser.getCompanyId();
            if(companyId != null){
                model.addAttribute("list",companyService.selectVideoCompanyById(companyId));
                return prefix + "/add";
            }
        }
        model.addAttribute("list",companyService.selectVideoCompanyList(null));
        return prefix + "/add";
    }

    /**
     * 新增保存模板
     */
//    @RequiresPermissions("video:template:add")
    @Log(title = "模板", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @ApiOperation("新增模板")
    public AjaxResult addSave(VideoTemplate videoTemplate)
    {
        String templateName = videoTemplate.getTemplateName();
        VideoTemplate v1 = new VideoTemplate();
        v1.setTemplateName(templateName);
        SysUser sysUser = ShiroUtils.getSysUser();
        if(sysUser != null){
            Integer companyId = sysUser.getCompanyId();
            if(companyId != null){
                v1.setCompanyId(companyId);
            }
        }
        List<VideoTemplate> list = videoTemplateService.selectVideoTemplateList(v1);
        if(CollectionUtils.isNotEmpty(list)){
            return error("模板名称重复");
        }
        return toAjax(videoTemplateService.insertVideoTemplate(videoTemplate));
    }

    /**
     * 修改模板
     */
//    @RequiresPermissions("video:template:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        VideoTemplate videoTemplate = videoTemplateService.selectVideoTemplateById(id);
        mmap.put("videoTemplate", videoTemplate);
        SysUser sysUser = ShiroUtils.getSysUser();
        if(sysUser != null){
            Integer companyId = sysUser.getCompanyId();
            if(companyId != null){
                mmap.put("list",companyService.selectVideoCompanyById(companyId));
                return prefix + "/edit";
            }
        }
        mmap.put("list",companyService.selectVideoCompanyList(null));
        return prefix + "/edit";
    }

//    @RequiresPermissions("video:template:edit")
    @GetMapping("/detail/{dictId}")
    public String detail(@PathVariable("dictId") Integer dictId, ModelMap mmap)
    {
        mmap.put("templateId", dictId);
//        mmap.put("dictList", detailService.selectDictTypeAll());
        return "video/detail/detail";
    }

    /**
     * 修改保存模板
     */
//    @RequiresPermissions("video:template:edit")
    @Log(title = "模板", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    @ApiOperation("修改模板")
    public AjaxResult editSave(VideoTemplate videoTemplate)
    {
        return toAjax(videoTemplateService.updateVideoTemplate(videoTemplate));
    }

    /**
     * 删除模板
     */
//    @RequiresPermissions("video:template:remove")
    @Log(title = "模板", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    @ApiOperation("删除模板")
    public AjaxResult remove(String ids)
    {
        return toAjax(videoTemplateService.deleteVideoTemplateByIds(ids));
    }
}
